package soap.latherqq.core.manage;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.event.MouseInputAdapter;

import org.apache.log4j.Logger;

import com.alee.utils.ImageUtils;

import soap.latherqq.core.LConstants;
import soap.latherqq.util.GifDecoder;
/**
 * 资源管理器
 * @author qiho2013
 *
 */
public class ResManage{
	private static final Logger log = Logger.getLogger(ResManage.class);
	private static ResManage self=null;
	public static synchronized ResManage self(){
		if(self==null){
			self=new ResManage();
		}
		return self;
	}
	private Map<String,Image> images;
	private ResManage(){
		
	}
	public void init(){
		images=new ConcurrentHashMap<String,Image>();
	}
	public ImageIcon getImageIcon(String file){
		try{
			Image temp=getImage(file);
			if(temp==null){
				return null;
			}
			return new ImageIcon(temp);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public Image getImage(String file){
		try{
			Image temp=images.get(file);
			if(temp!=null){
				return temp;
			}else{
				temp=ImageIO.read(new File(LConstants.RES_FILE+"/image/"+file));
				images.put(file, temp);
				return temp;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public Map<String, Image> getImages() {
		return images;
	}
	public static class ImageUtil{
		public static final ImageIcon setImageIconSize(ImageIcon icon,int width,int heigth){
			icon.setImage(icon.getImage().getScaledInstance(width, heigth, Image.SCALE_DEFAULT));
			return icon;
		}
		
		
		public static ImageIcon getFrameByGif(int iconId, int f) {
			GifDecoder decoder = new GifDecoder();
			try {
				decoder.read(new FileInputStream(new File(LConstants.RES_FILE+"/image/face/"+iconId+".gif")));
				System.out.println(LConstants.RES_FILE+"/image/face/"+iconId+".gif"+":"+decoder.getFrameCount());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return new ImageIcon(decoder.getFrame(f)); // 得到帧
		}
		public static void drawBlueBorder(final JComponent jc){
			jc.setBorder(BorderFactory.createLineBorder(new Color(212,221,232)));
			jc.addMouseListener(new MouseInputAdapter() {
				@Override
				public void mouseEntered(MouseEvent e) {
					JComponent ss=(JComponent) e.getSource();
					ss.setBorder(BorderFactory.createLineBorder(new Color(73,172,231)));
				}
				@Override
				public void mouseExited(MouseEvent e) {
					JComponent ss=(JComponent) e.getSource();
					ss.setBorder(BorderFactory.createLineBorder(new Color(212,221,232)));
				}
			});
		}
		public static BufferedImage drawStatusFace(BufferedImage face, BufferedImage stat) {
			BufferedImage canvas = ImageUtils.createCompatibleImage(face);
			Graphics2D g2d = canvas.createGraphics();
			g2d.drawImage(face, 0, 0, null);
			g2d.drawImage(stat, canvas.getWidth() - stat.getWidth(), canvas.getHeight() - stat.getHeight(), null);
			g2d.dispose();
			return canvas;
		}
		
		public static BufferedImage grayFace(BufferedImage raw){
			BufferedImage gray = new BufferedImage( raw.getWidth(), raw.getHeight(), BufferedImage.TYPE_BYTE_GRAY );  
			ColorConvertOp op = new ColorConvertOp( raw.getColorModel().getColorSpace(), gray.getColorModel().getColorSpace(), null );
			op.filter( raw, gray );
			return gray;
		}
	}
}
